public class test06 {
    /**
     * Definition for a binary tree node.
     * public class TreeNode {
     *     int val;
     *     TreeNode left;
     *     TreeNode right;
     *     TreeNode() {}
     *     TreeNode(int val) { this.val = val; }
     *     TreeNode(int val, TreeNode left, TreeNode right) {
     *         this.val = val;
     *         this.left = left;
     *         this.right = right;
     *     }
     * }
     */
        public boolean isValidBST(TreeNode root) {
            long lower = Long.MIN_VALUE;
            long upper = Long.MAX_VALUE;
            return isValid(root,lower,upper);
        }
        private boolean isValid(TreeNode root,long lower,long upper){
            if(root==null){
                return true;
            }
            if(root.val>=upper || root.val<=lower){
                return false;
            }
            return isValid(root.left,lower,root.val) && isValid(root.right,root.val,upper);
        }

}
